package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.*;
import com.atguigu.gmall.pms.feign.SmsClient;
import com.atguigu.gmall.pms.service.*;
import com.atguigu.gmall.pms.vo.SkuVo;
import com.atguigu.gmall.pms.vo.SpuBaseAttrVo;
import com.atguigu.gmall.pms.vo.SpuVo;
import com.atguigu.gmall.sms.vo.SkuSaleVo;
import io.jsonwebtoken.lang.Collections;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.SpuMapper;


@Service("spuService")
public class SpuServiceImpl extends ServiceImpl<SpuMapper, SpuEntity> implements SpuService {

    @Autowired
    private SpuDescService spuDescService;
    @Autowired
    private SpuAttrValueService spuAttrValueService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SkuAttrValueService skuAttrValueService;
    @Autowired
    private SmsClient smsClient;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SpuEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SpuEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public PageResultVo getSpuByCategoryId(Long categoryId, PageParamVo pageParamVo) {
        QueryWrapper<SpuEntity> wrapper = new QueryWrapper<>();
        if (categoryId != 0){
            wrapper.eq("category_id",categoryId);
        }
        String key = pageParamVo.getKey();
        if (StringUtils.isNotBlank(key)){
            wrapper.and(w->{
                w.eq("id",key).or().like("name",key);
            });
        }

        IPage<SpuEntity> iPage = this.page(pageParamVo.getPage(), wrapper);

        return new PageResultVo(iPage);
    }

    @GlobalTransactional
    @Override
    public void bigSave(SpuVo spuVo) {
        //1.保存spu相关信息
         //1.1 保存pms_spu
        SpuEntity spuEntity = new SpuEntity();
        BeanUtils.copyProperties(spuVo,spuEntity);
        spuEntity.setCreateTime(new Date());
        spuEntity.setUpdateTime(spuEntity.getCreateTime());
        this.save(spuEntity);
        Long spuId = spuEntity.getId();

        //1.2 保存pms_spu_desc
        SpuDescEntity spuDescEntity = new SpuDescEntity();
        spuDescEntity.setSpuId(spuId);
        List<String> spuImages = spuVo.getSpuImages();
        if (!Collections.isEmpty(spuImages)){
            String desc = StringUtils.join(spuImages, ",");
            spuDescEntity.setDecript(desc);
            spuDescEntity.setSpuId(spuId);
            spuDescService.save(spuDescEntity);
        }

        //1.3 保存pms_spu_attr_value
        List<SpuBaseAttrVo> baseAttrs = spuVo.getBaseAttrs();
        if (!Collections.isEmpty(baseAttrs)){
            List<SpuAttrValueEntity> spuAttrValueEntities = baseAttrs.stream().map(b -> {
                SpuAttrValueEntity spuAttrValueEntity = new SpuAttrValueEntity();
                BeanUtils.copyProperties(b, spuAttrValueEntity);
                spuAttrValueEntity.setSpuId(spuId);
                return spuAttrValueEntity;
            }).collect(Collectors.toList());
            spuAttrValueService.saveBatch(spuAttrValueEntities);
        }


        //2.保存sku相关信息
        //2.1 保存pms_sku
        List<SkuVo> skus = spuVo.getSkus();
        if (!Collections.isEmpty(skus)){
            skus.forEach(skuVo -> {
                SkuEntity skuEntity = new SkuEntity();
                skuEntity.setSpuId(spuId);
                skuEntity.setBrandId(spuVo.getBrandId());
                skuEntity.setCategoryId(spuVo.getCategoryId());
                List<String> skuVoImages = skuVo.getImages();
                if (!Collections.isEmpty(skuVoImages)) {
                    skuEntity.setDefaultImage(skuVo.getDefaultImage() == null
                            ? skuVoImages.get(0) : skuVo.getDefaultImage());
                }
                skuEntity.setName(skuVo.getName());
                skuEntity.setTitle(skuVo.getTitle());
                skuEntity.setSubtitle(skuVo.getSubtitle());
                skuEntity.setPrice(skuVo.getPrice());
                skuEntity.setWeight(skuVo.getWeight());

                skuService.save(skuEntity);
                Long skuId = skuEntity.getId();


                //2.2 保存pms_sku_image
                if (!Collections.isEmpty(skuVoImages)) {
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                    skuImagesEntity.setUrl(StringUtils.join(skuVoImages, ","));
                    skuImagesEntity.setSkuId(skuId);
                    skuImagesService.save(skuImagesEntity);
                }

                //2.3 保存pms_sku_attr_value
                List<SkuAttrValueEntity> saleAttrs = skuVo.getSaleAttrs();
                if (!Collections.isEmpty(saleAttrs)){
                     saleAttrs.forEach(attr -> {
                         attr.setSkuId(skuId);
                    });
                    skuAttrValueService.saveBatch(saleAttrs);
                }

                SkuSaleVo saleVo = new SkuSaleVo();
                BeanUtils.copyProperties(skuVo,saleVo);
                saleVo.setLaAddOther(skuVo.getLadderAddOther());
                saleVo.setSkuId(skuId);


                smsClient.saveSales(saleVo);

                // int i = 1/0;

            });
        }
    }

}